package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysZxCategory;
import com.ruoyi.system.service.ISysZxCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯分类维护Controller
 * 
 * @author ruoyi
 * @date 2024-04-03
 */
@RestController
@RequestMapping("/system/zxcategory")
public class SysZxCategoryController extends BaseController
{
    @Autowired
    private ISysZxCategoryService sysZxCategoryService;
    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询资讯分类维护列表
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysZxCategory sysZxCategory)
    {
        startPage();
        List<SysZxCategory> list = sysZxCategoryService.selectSysZxCategoryList(sysZxCategory);
        return getDataTable(list);
    }

    /**
     * 导出资讯分类维护列表
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:export')")
    @Log(title = "资讯分类维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysZxCategory sysZxCategory)
    {
        List<SysZxCategory> list = sysZxCategoryService.selectSysZxCategoryList(sysZxCategory);
        ExcelUtil<SysZxCategory> util = new ExcelUtil<SysZxCategory>(SysZxCategory.class);
        util.exportExcel(response, list, "资讯分类维护数据");
    }

    /**
     * 获取资讯分类维护详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:query')")
    @GetMapping(value = "/{zxCategoryId}")
    public AjaxResult getInfo(@PathVariable("zxCategoryId") Long zxCategoryId)
    {
        return success(sysZxCategoryService.selectSysZxCategoryByZxCategoryId(zxCategoryId));
    }

    /**
     * 新增资讯分类维护
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:add')")
    @Log(title = "资讯分类维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysZxCategory sysZxCategory)
    {
        //将字典 对应中文保存起来
        if (!StringUtils.isEmpty(sysZxCategory.getCateTypeid())){
            SysDictData sysDictData=new SysDictData();
            sysDictData.setDictType("sys_zx_content_type");
            sysDictData.setDictValue(sysZxCategory.getCateTypeid());
            List<SysDictData>  list= dictDataService.selectDictDataList(sysDictData);
            if (list!=null&&list.size()>0){
                sysZxCategory.setCateText(list.get(0).getDictLabel());
            }
        }
        return toAjax(sysZxCategoryService.insertSysZxCategory(sysZxCategory));
    }

    /**
     * 修改资讯分类维护
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:edit')")
    @Log(title = "资讯分类维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysZxCategory sysZxCategory)
    {
        //将字典 对应中文保存起来
        if (!StringUtils.isEmpty(sysZxCategory.getCateTypeid())){
            SysDictData sysDictData=new SysDictData();
            sysDictData.setDictType("sys_zx_content_type");
            sysDictData.setDictValue(sysZxCategory.getCateTypeid());
          List<SysDictData>  list= dictDataService.selectDictDataList(sysDictData);
          if (list!=null&&list.size()>0){
              sysZxCategory.setCateText(list.get(0).getDictLabel());
          }
        }

        return toAjax(sysZxCategoryService.updateSysZxCategory(sysZxCategory));
    }

    /**
     * 删除资讯分类维护
     */
    @PreAuthorize("@ss.hasPermi('system:zxcategory:remove')")
    @Log(title = "资讯分类维护", businessType = BusinessType.DELETE)
	@DeleteMapping("/{zxCategoryIds}")
    public AjaxResult remove(@PathVariable Long[] zxCategoryIds)
    {
        return toAjax(sysZxCategoryService.deleteSysZxCategoryByZxCategoryIds(zxCategoryIds));
    }
}
